/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
Nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "kakikata_sheet.h"
#include "kanji.h"

int KakikataSheet::
count_pagine ( void )
{
	return ( _kanji->size() / 40 ) + ( ( _kanji->size() % 40 ) > 0 );
}

void KakikataSheet::
set_up_kanji_table ( const Cairo::RefPtr<Cairo::Context>& cr )
{
	cr->set_line_width(2.0);
	cr->select_font_face ( "TakaoGothic", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL );
	cr->set_font_size ( 28.0 );
	cr->set_source_rgb(0.0, 0.0, 0.0);
}

void KakikataSheet::
draw_kanji_grid ( const Cairo::RefPtr<Cairo::Context>& cr, double dy )
{
	cr->move_to (xLeft, yUp + dy);
	cr->line_to (xLeft, pHeight - yDown + dy);
	cr->line_to (pWidth - xRight, pHeight - yDown + dy);
	cr->line_to (pWidth - xRight, yUp + dy);
	cr->line_to (xLeft, yUp + dy);
	for(short i = 1; i < 20; i++)
	{
		cr->move_to (xLeft, yUp + (cHeight * i) + dy);
		cr->line_to (pWidth - xRight, yUp + (cHeight * i) + dy);
	}
	for(short i = 1; i < 12; i++)
	{
		cr->move_to (xLeft + (cWidth * i), yUp + dy);
		cr->line_to (xLeft + (cWidth * i), pHeight - yDown + dy);
	}
	cr->stroke ();
}

void KakikataSheet::
draw_kanji_at_page ( const Cairo::RefPtr<Cairo::Context>& cr, int pagina, bool pages )
{
	double dy = pages ? pHeight * pagina : 0.0;
	Cairo::TextExtents te;
	list <Kanji>::iterator _itKnj = _kanji->begin();
	list <Kanji>::iterator _itEnd = _kanji->end();
	for ( int nk = 0; nk < ( pagina * 40 ); nk++ )
		_itKnj++;
	for ( short nk = 1; nk < 41; nk++ )
	{
		if ( _itKnj != _itEnd )
		{
			cr->get_text_extents ( _itKnj->get_kanji().raw(), te );
			int x = nk < 21 ? xLeft + ( ( cWidth - te.x_advance ) / 2 ) : xLeft + ( cWidth * 6 ) + ( ( cWidth - te.x_advance ) / 2 );
			int y = nk < 21 ? yUp + ( cHeight * nk ) - ( ( cHeight - te.height ) / 2 ) + dy : yUp + ( cHeight * ( nk - 20 ) ) - ( ( cHeight - te.height ) / 2 ) + dy;
			cr->move_to ( x, y );
			cr->show_text ( _itKnj->get_kanji().raw() );
			_itKnj++;
		}
		if ( _itKnj == _itEnd )
			break;		
	}
}
